#ifndef SETTINGIMPL_H
#define SETTINGIMPL_H

#include "room_global.h"
#include "datatype.h"
#include "../../core/src/log.h"
#include <boost/noncopyable.hpp>
#include <boost/filesystem.hpp>

template< typename T >
class SettingImpl :
        protected ServerSettingData,
        protected NetSettingData,
        protected TableSettingData,
        protected DatabaseSettingData,
        protected boost::noncopyable
{
public:
    SettingImpl()
    {

    }

    void release()
    {
        T *pt = static_cast< T * >(this);

        delete pt;
    }

    bool  init(const std::string &sf)
    {
        T *pt = static_cast< T * >(this);

        if(sf.empty()){
            LOG_WARNING() << "Invlaid setting file path!, Use default.";
            boost::filesystem::path c = boost::filesystem::current_path();
            c = c / "room.xml";
            m_sf = c.string();
        }else{
            boost::filesystem::path p(sf);

            if(boost::filesystem::is_directory(p)){
                p = p / "room.xml";

                if(!boost::filesystem::exists(p)){
                    LOG_WARNING() << "Invalid setting file path! Use default.";
                    boost::filesystem::path c = boost::filesystem::current_path();
                    c = c / "room.xml";
                    m_sf = c.string();
                }else{
                    m_sf = p.string();
                }
            }else{
                if(boost::filesystem::exists(p)){
                    m_sf = p.string();
                }else{
                    LOG_WARNING() << "Invlaid setting file path!, Use default.";
                    boost::filesystem::path c = boost::filesystem::current_path();
                    c = c / "room.xml";
                    m_sf = c.string();
                }
            }
        }


        LOG_TRACE() << "Setting file path: " << m_sf;

        return pt->readSettings();
    }

    template< typename DT >
    const DT *settingData()
    {
        return static_cast< DT * >(this);
    }

protected:
    std::string m_sf;
};

#endif // ANSETTINGIMPL_H
